package com.hr.salary.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hr.salary.domain.Salary;
import com.hr.salary.domain.SalaryRS;
import com.hr.salary.service.impl.SalaryServiceImpl;
import com.hr.user.domain.User;
import com.hr.user.service.Impl.UserServiceImpl;
import com.hr.utils.BaseServlet;
import com.hr.utils.CommonUtils;
import com.hr.utils.PageBean;

public class SalaryServlet extends BaseServlet {
	private SalaryServiceImpl salaryServiceImpl = new SalaryServiceImpl();
	private UserServiceImpl userServiceImpl = new UserServiceImpl();
	
	/**
	 * 获取当前的页面值
	 * @param request
	 * @return
	 */
	private int getPageCurrent(HttpServletRequest request){
		String value = request.getParameter("PageCurrent");
		if(value == null || value.trim().isEmpty()){
			return 1;
		} else{
			return Integer.parseInt(value);
		}
	}
	
	/**
	 * 截取url
	 * @param request
	 * @return
	 */
	public String getUrl(HttpServletRequest request){
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
		String queryString = request.getQueryString();//获取问号之后的参数部份
		
		//  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
		if(queryString.contains("&PageCurrent=")) {
			int index = queryString.lastIndexOf("&PageCurrent=");
			queryString = queryString.substring(0, index);
		}
		
		return contextPath + servletPath + "?" + queryString;
	}
	
	/**
	 * 查询所有工资信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryAllSalaryInfo(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		PageBean<Salary> pageBean = salaryServiceImpl.queryAll_Page(PageCurrent, PageSize);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/salary/salary_list_info.jsp";
	}
	
	/**
	 * 添加奖惩管理准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addSalaryRPPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setAttribute("list_user", userServiceImpl.queryAllUser());
		return "f:/salary/salary_add_by_manager.jsp";
	}
	
	/**
	 * 添加奖惩项
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addSalaryRP(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String user_id = request.getParameter("user_id");
		String salary_id = salaryServiceImpl.findByUserID(user_id);
		String username = userServiceImpl.findByUserID(user_id).getUsername();
		String pro_name = request.getParameter("pro_name");
		String pro_describle = request.getParameter("pro_describle");
		String rp = request.getParameter("rp");
		String pro_money_str = rp + request.getParameter("pro_money");
		double pro_money = Double.parseDouble(pro_money_str);
		String month = request.getParameter("month");
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		int year = c.get(Calendar.YEAR); 
		Object[] params = {salary_id,user_id,pro_name,pro_describle,pro_money,
				"0",month,year,CommonUtils.uuid()};
		salaryServiceImpl.addSalaryRP(params);
		request.setAttribute("msg", "您为员工【"+username+"】 添加的 【"+pro_name+"】 正式进入审核流程");
		return "f:/SalaryServlet?method=queryAllSalaryInfo";
	}
	
	/**
	 * 审查前准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String salaryServletCheckPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String salary_id = request.getParameter("salary_id");
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		PageBean<SalaryRS> pageBean = salaryServiceImpl.queryAll_Page_SalaryRS(PageCurrent, PageSize, salary_id);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/salary/salary_list_check_info.jsp";
	}
	
	/**
	 * 通过审查，并且进行工资的修改
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String salaryServletCheckPass(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String pro_id = request.getParameter("pro_id");
		String pro_name = request.getParameter("pro_name");
		String pro_money = request.getParameter("pro_money");
		String salary_id = request.getParameter("salary_id");
		String user_id = request.getParameter("user_id");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		salaryServiceImpl.checkPass(pro_id);
		salaryServiceImpl.checkPassUpdateSalaryReal(salary_id,user_id,pro_money,month,year);
		pro_name = new String(pro_name.getBytes("ISO-8859-1"), "utf-8");
		request.setAttribute("msg", "  ||  【"+pro_name+"】 奖惩项目审核通过成功！");
		return "f:/SalaryServlet?method=salaryServletCheckPre";
	}
	
	/**
	 * 彻底否决提案
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String salaryServletCheckRefuse(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String pro_id = request.getParameter("pro_id");
		String pro_name = request.getParameter("pro_name");
		String pro_money = request.getParameter("pro_money");
		String salary_id = request.getParameter("salary_id");
		String user_id = request.getParameter("user_id");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		salaryServiceImpl.checkRefuse(pro_id);
		salaryServiceImpl.checkRefuseUpdateSalaryReal(salary_id,user_id,pro_money,month,year);
		pro_name = new String(pro_name.getBytes("ISO-8859-1"), "utf-8");
		request.setAttribute("msg", " 【"+pro_name+"】 奖惩项目审核否决成功！");
		return "f:/SalaryServlet?method=salaryServletCheckPre";
	}
	
	/**
	 * 修改基本工资前的准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateSalaryPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String salary_id = request.getParameter("salary_id");
		String user_id = request.getParameter("user_id");
		Salary salary = salaryServiceImpl.findBySalaryId(salary_id);
		User user = userServiceImpl.findByUserID(user_id);
		request.setAttribute("salary", salary);
		request.setAttribute("user", user);
		return "f:/salary/salary_edit.jsp";
	}
	
	/**
	 * 修改基础工资
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateSalaryBasic(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
		String salary_id = request.getParameter("salary_id");
		String user_id = request.getParameter("user_id");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String salary_basic = request.getParameter("salary_basic");
		double salary_b = Double.parseDouble(salary_basic);
		Object[] params = {salary_b,salary_b,salary_id,user_id,month,year};
		salaryServiceImpl.updateSalary(params);
		request.setAttribute("msg", "修改基本工资成功");
		return "f:/SalaryServlet?method=queryAllSalaryInfo";
	}
	
	/**
	 * 申请奖金页面前的准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addApplyByPersonPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		PageBean<SalaryRS> pageBean = salaryServiceImpl.queryAll_Page_SalaryRS(PageCurrent, PageSize, user);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/salary/salary_list_apply_info.jsp";
	}
	
	/**
	 * 个人工资的变更记录
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addApplyByPersonList(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		PageBean<SalaryRS> pageBean = salaryServiceImpl.queryAll_Page_SalaryRS(PageCurrent, PageSize, user);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/salary/salary_list_change_record.jsp";
	}
	
	/**
	 * 查询实际奖金
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String salaryByOwn(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		List<Salary> list_salary = salaryServiceImpl.findSalaryByUserID(user.getUser_id());
		request.setAttribute("list_salary", list_salary);
		request.setAttribute("user", user);
		return "f:/salary/salary_real_now.jsp";
	}
	
	/**
	 * 提交奖金申请的准备（个人）
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addSalaryApplyRPPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		request.setAttribute("user", user);
		return "f:/salary/salary_apply.jsp";
	}
	
	/**
	 * 个人提交奖金申请
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addSalaryApplyByPerson(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String user_id = request.getParameter("user_id");
		String salary_id = salaryServiceImpl.findByUserID(user_id);
		String username = userServiceImpl.findByUserID(user_id).getUsername();
		String pro_name = request.getParameter("pro_name");
		String pro_describle = request.getParameter("pro_describle");
		String pro_money_str = request.getParameter("pro_money");
		double pro_money = Double.parseDouble(pro_money_str);
		String month = request.getParameter("month");
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		int year = c.get(Calendar.YEAR); 
		Object[] params = {salary_id,user_id,pro_name,pro_describle,pro_money,
				"0",month,year,CommonUtils.uuid()};
		salaryServiceImpl.addSalaryRP(params);
		request.setAttribute("msg", "【"+username+"】您刚才进行添加的 【"+pro_name+"】奖金项目 正式进入审核流程,请等待审批");
		return "f:/SalaryServlet?method=addApplyByPersonList";
	}
	
	/**
	 * 添加基础工资前的准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addSalaryInitPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		List<User> list_user = userServiceImpl.queryAllUser();
		request.setAttribute("list_user", list_user);
		return "f:/salary/salary_add_init.jsp";
	}
	
	/**
	 * 添加员工基本工资
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addSalaryInit(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		Salary salary = CommonUtils.toBean(request.getParameterMap(), Salary.class);
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		salary.setYear(year+"");
		
		String salary_id_find = salaryServiceImpl.findByUserID(salary.getUser_id());
		Salary salary_find = salaryServiceImpl.findBySalaryId(salary_id_find);
		if(salary_find.getMonth().equalsIgnoreCase(salary.getMonth()) && salary_find.getYear().equalsIgnoreCase(salary.getYear())){
			List<User> list_user = userServiceImpl.queryAllUser();
			request.setAttribute("msg","您已经添加过该员工今年这个月的基本薪资");
			request.setAttribute("list_user", list_user);
			return "f:/salary/salary_add_init.jsp";
		} else {
			String salary_id = CommonUtils.uuid();
			salary.setSalary_id(salary_id);
			salaryServiceImpl.addSalary(salary);
			return "f:/SalaryServlet?method=queryAllSalaryInfo";
		}
	}
	
	/**
	 * 删除申请记录
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String salaryServletApplyDelete(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		//pro_id=${salaryRs.pro_id}
		//pro_name=${salaryRs.pro_name}
		//pro_money=${salaryRs.pro_money}
		//salary_id=${salaryRs.salary_id}
		//user_id=${salaryRs.user_id}
		//month=${salaryRs.month}
		//year=${salaryRs.year}
		String pro_id = request.getParameter("pro_id");
		String pro_name = request.getParameter("pro_name");
		String pro_money = request.getParameter("pro_money");
		String salary_id = request.getParameter("salary_id");
		String user_id = request.getParameter("user_id");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String pro_name_utf = new String(pro_name.getBytes("ISO-8859-1"),"utf-8"); 
		salaryServiceImpl.deleteApplyByPerson(pro_id,salary_id,user_id,month,year);
		request.setAttribute("msg", "您刚才进行删除的 【"+pro_name_utf+"】奖金项目已经删除成功");
		return "f:/SalaryServlet?method=addApplyByPersonList";
	}
}
